
Page({
    data: {
         goodsList:[],
         orderName:'李先生',
         orderPhone:'186****3980',
         orderAddress:'北京市昌平区回龙观天通苑一区5号楼三单元' ,
         address:null,
         arr:[],
         isNoReload:false,
         userCouponId:'',
         isShow:false

        
    },
    onLoad:function(opt){
      console.log(2)
     var userInfo= wx.getStorageSync('userInfo');
        this.setData({
            userInfo:userInfo
     })
      if(opt.type=='hide'){
        wx.setStorageSync('shopAddress',null)
      }
      
    },
     getMyConpon(){

      console.log('*************')
      console.log('*******获取优惠券******')
      
       var addrId=null;
       if (this.data.address) {
        addrId= this.data.address.id
       }
        var _self=this;
        wx.request({
            url: getApp().globalData.baseUrl + '/yx/userCoupon/getUseList',
            method:"get",
            data:{
                openid:this.data.userInfo.openid,
                status:1,
                addrId:addrId
            },
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log('*******获取优惠券成功******')
               console.log(res)
               
               var  arr1=[];
               var  arr2=[];
               for (var i = 0; i < res.data.obj.length; i++) {
                 if (res.data.obj[i].isuse==1) {
                  arr1.push(res.data.obj[i])
                 }else{
                  arr2.push(res.data.obj[i])
                 }
               }
               var arr = arr1.concat(arr2);

               _self.setData({
                  isShow:true,
                  couponList:arr
               })
                               
             }
            }) 

     },
    onShow:function(){
     
       if (this.data.isNoReload) {
        return
       }

       var address= wx.getStorageSync('shopAddress');
       console.log("address")
       console.log(address)
       var _self=this;
       
       if (!address) {
         this.getAddress();
         
       }else{
        
       }

       this.setData({
          address:address
       })
      
      this.creatOrder();
      // this.getMyConpon();

    },
     onHide: function() {
      var address= wx.setStorageSync('shopAddress',null);
      // 页面从前台变为后台时执行
      console.log(' 页面从前台变为后台时执行')
      // wx.setStorageSync('shopAddress',null)
      },
      onUnload: function() {
        // 页面销毁时执行
      console.log(' 页面销毁时执行')
      },
    getAddress:function(){
        var _self=this;
        wx.request({
            url:getApp().globalData.baseUrl+'/addr/list',
            method:"get",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            data:{openid:this.data.userInfo.openid},
            success: function(res) {
              if (res.data.code==200) {

               if (res.data.obj.length>0) {
                  for(var i=0;i<res.data.obj.length;i++){
                  if (res.data.obj[i].del=='1') {

                      _self.setData({
                        address:res.data.obj[i]
                      })
                  }
                }
                _self.creatOrder();
               }else{
                
                     wx.showModal({
                      title: '提示',
                      cancelText:'取消',
                      confirmText:'去设置',
                      content: '您还没有收获地址哦, 快去设置一个吧!',
                      success (res) {
                        if (res.confirm) {
                          console.log('用户点击确定')
                          _self.chooseAddress();
                        } else if (res.cancel) {
                          console.log('用户点击取消') 
                        }
                      }
                    })
                

               } 
               
              }
         }
       })
    },
    getShopMarket:function(){
        var _self=this;
          wx.request({
            url: getApp().globalData.baseUrl + '/pj/shopping/find?openid='+this.data.userInfo.openid,
            method:"GET",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
               if (res.data.code==200) {
                
              _self.setData({
                    goodsList:res.data.obj
                 })

               }else{
                 
               }
                
                                      
             }
            }) 
    },
    chooseGoods:function(e){
     console.log(e.currentTarget.dataset.item);
     console.log(e.currentTarget.dataset.companyid);
     var arr=this.data.goodsList;
     for(var i=0;i<arr.supplierCartVos.length;i++){
        console.log('merchantId='+arr.supplierCartVos[i].merchantId)
         if (arr.supplierCartVos[i].merchantId==e.currentTarget.dataset.companyid) {
            console.log('命中1')
            for(var j=0;j<arr.supplierCartVos[i].supplierCartParts.length;j++){
               
                if (arr.supplierCartVos[i].supplierCartParts[j].type==2) {
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id&&arr.supplierCartVos[i].supplierCartParts[j].normsId==e.currentTarget.dataset.item.normsId) {
                      arr.supplierCartVos[i].supplierCartParts[j].status=arr.supplierCartVos[i].supplierCartParts[j].status=='0'?"1":"0"
                   } 
                }else{
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id) {
                      arr.supplierCartVos[i].supplierCartParts[j].status=arr.supplierCartVos[i].supplierCartParts[j].status=='0'?"1":"0"
                   } 
                }

             
            }
         }
     }
     console.log(arr)
     this.setData({
        goodsList:arr
     })
     this.getMoney();
    },
    getMoney:function(){
            var _self=this;
            var arr=[];
            for(var i=0;i<this.data.goodsList.supplierCartVos.length;i++){
               for(var j=0;j<this.data.goodsList.supplierCartVos[i].supplierCartParts.length;j++){
                     var json={
                        "id":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].id,
                        "merchantId":this.data.goodsList.supplierCartVos[i].merchantId,//供应商ID:"GYS771337000452",
                        "type":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].type,//商品类型：1普通商品 2规格商品,
                        "normsId":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].normsId,//规格ID（如果type==1,就不需要传）,
                        "num":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].num,//数量,
                        "status":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].status//勾选状态：0没勾选 1已勾选
                    }
                   arr.push(json) 
               }  
            }
            console.log(arr)
       
            wx.request({
            url: getApp().globalData.baseUrl + '/pj/shopping/countCart',
            method:"POST",
            data:{
                openid:this.data.userInfo.openid,
                pjCountCartRequestListVo:arr
            },
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
                if (res.data.code==200) {
                 _self.setData({
                    goodsList:res.data.obj
                 })
               }
                               
            }
            }) 
    },
    chooseAddress:function(){
         wx.navigateTo({
        url: '/pages/address/address'
       })
    },
    creatOrder:function(){
      console.log('*************')
      console.log('*******计算购物车******')
      
       var addrId=null;
       if (this.data.address) {
        addrId= this.data.address.id
       }
        var _self=this;
        wx.request({
            url: getApp().globalData.baseUrl + '/pj/shopping/settlement',
            method:"POST",
            data:{
                openid:this.data.userInfo.openid,
                addrId:addrId,
                userCouponId:this.data.userCouponId
            },
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
                if (res.data.code==200) {
                   _self.setData({
                    goodsList:res.data.obj
                 })
                   
               }else{
                 _self.setData({
                    goodsList:res.data.obj
                 })
                //   wx.showToast({
                //   title: res.data.des,
                //   icon:'none',
                //   duration: 2000
                // })
                 wx.showModal({
                      title: '提示',
                      cancelText:'取消',
                      confirmText:'重新选择',
                      content: res.data.des,
                      success (res) {
                        if (res.confirm) {
                          console.log('用户点击确定')
                          _self.chooseAddress();
                        } else if (res.cancel) {
                          console.log('用户点击取消') 
                        }
                      }
                    })
               }
                               
             }
            }) 
    },
    closeShow(){
       this.setData({
        isShow:false
       })
    },
    showCoupon(){
       this.getMyConpon();
    },
    creatOrder2:function(){
       var arr=[];
       var arr1=this.data.arr;

       for(var i=0;i<this.data.goodsList.supplierCartVos.length;i++){
         var isTrue=true;
         for(var j=0;j<arr1.length;j++){
           if (this.data.goodsList.supplierCartVos[i].merchantId==arr1[j].merchantId) {
            arr.push(arr1[j])
            isTrue=false;
           }
        }
        console.log(isTrue)
        if (isTrue||arr1.length==0) {
          arr.push({
             merchantId:this.data.goodsList.supplierCartVos[i].merchantId,
             remark:''
          })
        }
        // arr.push(json)
       }
       console.log(arr);
       var _self=this;
       // return
        wx.request({
            url: getApp().globalData.baseUrl + '/pj/order/submit',
            method:"POST",
            data:{
                openid:this.data.userInfo.openid,
                addrId:this.data.address.id,
                pjOrderRemarkVo:arr,
                userCouponId:this.data.userCouponId
  
            },
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
                if (res.data.code==200) {
                    wx.requestPayment({
                      timeStamp: res.data.obj.timeStamp,
                      nonceStr: res.data.obj.nonceStr,
                      package: res.data.obj.package,
                      signType: 'MD5',
                      paySign: res.data.obj.paySign,
                      success (res) { 
                        console.log('成功')
                        _self.setData({
                          isNoReload:true
                        })  
                         wx.redirectTo({
                          url: '/pages/order/order'
                         })
                      },
                      fail (res) {
                        console.log('失败')
                         _self.setData({
                          isNoReload:true
                        }) 
                        wx.redirectTo({
                          url: '/pages/order/order'
                         })
                       }
                    })
               }
                               
             }
            }) 
    },
    textContentSeller:function(e){
      console.log(e)   
      var conpony=e.currentTarget.dataset.compony;
      var text=e.detail.value;
      var arr=this.data.arr;
      if (arr.length==0) {
        arr.push({
          merchantId:conpony,
          remark:text
        })
       
      }else{
       var isTrue=true;
       for (var i = 0; i < arr.length; i++) {
        if (arr[i].merchantId==conpony) {
          console.log('修改')
           arr[i].remark=text;
           isTrue=false
        }
       }
       if(isTrue){
        console.log('新增')
        arr.push({
          merchantId:conpony,
          remark:text
        })
       }

      }
       this.setData({
          arr:arr
        })

    },
    chooseCoupon(e){
      console.log(e.currentTarget.dataset.item)
      var item=e.currentTarget.dataset.item
      if (item.isuse==0) {
       wx.showToast({
        title: item.isuseinfo,
        icon: 'none',
        duration: 2000
      })
        return
      }


      if (this.data.userCouponIda==item.id) {
         this.setData({
          userCouponIda:null,
          userCouponNamea:null
        })
      }else{
         this.setData({
          userCouponIda:item.id,
          userCouponNamea:item.reduceAmt
      })
      }
     
    },
    makeSure(){
     this.setData({
          userCouponId:this.data.userCouponIda,
          userCouponName:this.data.userCouponNamea,
          isShow:false
      })
     this.creatOrder();
    }

});